<?php

namespace App\Containers\AppSection\User\Actions;

use App\Containers\AppSection\BankDetail\Tasks\CreateDefaultBankDetailTask;
use App\Containers\AppSection\Confirmation\Tasks\DeleteAllConfirmsByValueTask;
use App\Containers\AppSection\Confirmation\Tasks\FindConfirmationByCodeTask;
use App\Containers\AppSection\Data\Tasks\CreateDefaultCompanyTask;
use App\Containers\AppSection\Data\Tasks\CreateDefaultPassportTask;
use App\Containers\AppSection\Data\Tasks\CreateDefaultPersonalTask;
use App\Containers\AppSection\LegalForm\Tasks\CreateDefaultLegalFormTask;
use App\Containers\AppSection\Log\Tasks\CreateLogTask;
use App\Containers\AppSection\Profile\Tasks\CreateProfileTask;
use App\Containers\AppSection\Questionnaire\Tasks\CreateDefaultQuestionnairesTask;
use App\Containers\AppSection\User\Models\User;
use App\Containers\AppSection\User\Tasks\CreateUserByCredentialsTask;
use App\Ship\Parents\Actions\Action;
use App\Ship\Parents\Requests\Request;
use Illuminate\Support\Facades\Hash;

class RegisterUserWithProfileAction extends Action
{
    public function run(Request $request): User
    {
        //Проверка кода телефона
        app(FindConfirmationByCodeTask::class)
            ->run($request->phone_code, $request->phone, config('appSection-confirmation.confirms.phone.requests.register'), config('appSection-confirmation.confirms.phone.type'));

        //Проверка кода почты
        app(FindConfirmationByCodeTask::class)
            ->run($request->email_code, $request->email, config('appSection-confirmation.confirms.email.requests.register'), config('appSection-confirmation.confirms.email.type'));

        //Создание пользователя
        $user = app(CreateUserByCredentialsTask::class)
            ->run([
                'email' => $request->email,
                'password' => Hash::make($request->password),
                'fio' => $request->fio,
                'status_key' => 'users_ok',
                'email_verified_at' => now(),
                'phone' => $request->getInputByKey('phone'),
                'phone_verified_at' => now(),
                'other_information' => '',
            ], $request->role);

        //Создание данных
        $passport = app(CreateDefaultPassportTask::class)->run($user->id);
        $personal = app(CreateDefaultPersonalTask::class)->run($user->id, $request->fio, $request->without_patronymic);
        $companyId = null;
        if ($request->legal_form_type === 'entity') {
            $company = app(CreateDefaultCompanyTask::class)->run($user->id, $request->getInputByKey('company'));
            $companyId = $company->id;
        }

        //Создание правовой формы
        $legalForm = app(CreateDefaultLegalFormTask::class)->run($user->id, $passport->id, $personal->id, $companyId);

        //Создание анкеты
        app(CreateDefaultQuestionnairesTask::class)
            ->run($request->legal_form_type, $legalForm->id);

        //Создание профиля
        $profile = app(CreateProfileTask::class)
            ->run(['user_id' => $user->id, 'phone' => $request->phone, 'legal_form_type' => $request->legal_form_type, 'legal_form_id' => $legalForm->id,]);

        //Создание банковских реквизитов
        app(CreateDefaultBankDetailTask::class)->run($profile->id);

        //Удаление отправленных кодов на телефон
        app(DeleteAllConfirmsByValueTask::class)
            ->run($request->phone, config('appSection-confirmation.confirms.phone.requests.register'), config('appSection-confirmation.confirms.phone.type'));

        //Удаление отправленных кодов на почту
        app(DeleteAllConfirmsByValueTask::class)
            ->run($request->email, config('appSection-confirmation.confirms.email.requests.register'), config('appSection-confirmation.confirms.email.type'));


        app(CreateLogTask::class)->run($profile->id, 'Регистрация', 'user');
        return $user;
    }
}
